package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.EnvironmentService;
import com.suyuan.utils.DeleteRows;
import com.suyuan.vo.EnvInforVO;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.EventObject;
import java.util.List;


@Service("environmentService")
public class EnvironmentServiceImpl implements EnvironmentService {

    @Resource
    private EnvInforMapper envInforMapper;

    @Resource
    private AreatypeinforMapper areatypeinforMapper;

    @Resource
    private AreacodeinforMapper areacodeinforMapper;

    @Resource
    private CompanyInfoMapper companyInfoMapper;

    @Resource
    private SensorTypeMapper sensorTypeMapper;

    /**
     * 多条件查询养殖环境信息
     * @param beginTime
     * @param endTime
     * @param aid
     * @param page
     * @return
     */
    public PageInfo findEnvironment(Date beginTime, Date endTime, Integer aid, Page page) {
        EnvInforExample envInforExample = new EnvInforExample();
        EnvInforExample.Criteria criteria = envInforExample.createCriteria();
        if (beginTime != null){
            criteria.andRecordtimeGreaterThan(beginTime);
        }
        if(endTime != null){
            criteria.andRecordtimeGreaterThan(endTime);
        }
        if(aid != null){
            criteria.andAreaidEqualTo(aid);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<EnvInfor> envInforList = envInforMapper.selectByExample(envInforExample);
        PageInfo pageInfo = new PageInfo(envInforList);
        List<EnvInforVO> envInforVOList = new ArrayList<EnvInforVO>();
        for (EnvInfor envInfor : (List<EnvInfor>)pageInfo.getList()){
            Areatypeinfor areatypeinfor = areatypeinforMapper.selectByPrimaryKey(envInfor.getEnvtypeid());
            CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(envInfor.getCompid());
            SensorType sensorType = sensorTypeMapper.selectByPrimaryKey(envInfor.getSensortypeid());
            envInforVOList.add(new EnvInforVO(envInfor,null,areatypeinfor,companyInfo,sensorType));
        }
        pageInfo.setList(envInforVOList);
        return pageInfo;
    }


    /**
     * 删除环境信息
     * @param selectRows
     * @return
     */
    @Transactional
    public Integer deleteEnvironInfo(String selectRows) {
        DeleteRows deleteRows = new DeleteRows();
        int [] deleteID = deleteRows.selectMore(selectRows);
        for (Integer ID : deleteID){
            envInforMapper.deleteByPrimaryKey(ID);
        }
        return 1;
    }


    /**
     * 添加指定环境信息
     * @param envInfor
     */
    public void addInfo(EnvInfor envInfor) {
        envInforMapper.insert(envInfor);
    }

    /**
     *根据环境信息ID删除环境信息
     * @param id
     * @return
     */
    public Integer deleteInfo(Integer id) {
        envInforMapper.deleteByPrimaryKey(id);
        return 1;
    }

    /**
     * 根据环境信息id查找指定的环境信息
     * @param gid
     * @return
     */
    public EnvInforVO findByGid(Integer gid) {
        EnvInfor envInfor =    envInforMapper.selectByPrimaryKey(gid);
        CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(envInfor.getCompid());
        SensorType sensorType  = sensorTypeMapper.selectByPrimaryKey(envInfor.getSensortypeid());
        EnvInforVO envInforVO = new EnvInforVO(envInfor,null,null,companyInfo,sensorType);
        return envInforVO;
    }


    /**
     * 更新环境信息
     * @param envInfor
     */
    public void updateInfo(EnvInfor envInfor) {
        EnvInfor envInfor1 = envInforMapper.selectByPrimaryKey(envInfor.getGid());
        envInfor1.setAreaid(envInfor.getAreaid());
        envInfor1.setEnvtypeid(envInfor.getEnvtypeid());
        envInfor1.setRecordtime(envInfor.getRecordtime());
        envInfor1.setSensortypeid(envInfor.getSensortypeid());
        envInfor1.setSensorvalue(envInfor.getSensorvalue());
        envInforMapper.updateByPrimaryKeySelective(envInfor1);
    }

    public List<CompanyInfo> findAllCompany() {
        return companyInfoMapper.selectByExample(null);
    }

    public List<SensorType> findAllSesenor() {
        return sensorTypeMapper.selectByExample(null);
    }
}
